﻿/// <summary>
/// 
///	Author:		    Gerard Finn
///
///	Student No		X00115840
///
///	Date:			Start Date 	:- 22-04-2014
///	Modified					:-
/// 
/// File            ServiceController.cs
///   
///	Description		Entry point for web service
///
/// </summary>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;

using Service.Models;

namespace Service.Controllers
{
    public class ServiceController : ApiController
    {
        CA1TableClient clientTable;
        /// <summary>
        /// 
        ///     Pre     :- None default constructor for controler class
        ///
        ///     Post    :- Init an instance of the table client class
        ///     
        /// </summary>
        public ServiceController() 
        {
            clientTable = new CA1TableClient();
        }
        /// <summary>
        /// 
        ///     Pre     :- The database connection must be open
        ///
        ///     Post    :- returns all items in the database
        ///     
        /// </summary>
        public IEnumerable<StockDetails> GetAllListings()
        {
            //
            // Need a cast here otherwise it is gving a 500 error
            //
            return (IEnumerable<StockDetails>)clientTable.GetAllListings();         // 200 OK, details serialized in response body
        }
        /// <summary>
        /// 
        ///     Pre     :- The database connection must be open
        ///                idInput must be a valid RowKey in database
        ///
        ///     Post    :- returns the required item from the database
        ///     
        ///                For some reason this goes to £$*^ if i dont return 
        ///                an IEnumerable on the Client side
        ///                
        ///                Only adding a single element but the client side detects the 
        ///                return as an array if i send back a stock details object
        ///     
        /// </summary>
        public IEnumerable<StockDetails> GetStockItem(String idInput)
        {
            StockDetails lst = clientTable.GetSingleItem(idInput);
            if (lst == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);       // return a http erro response status code of 404
            }
            // 200 return if we get here
            List<StockDetails> ret = new List<StockDetails>();
            // 
            // Add single element to list and return it 
            //
            ret.Add(lst);
            return ret;
        }
        /// <summary>
        /// 
        ///     Pre     :- The database connection must be open
        ///                idInput must be a valid RowKey in database
        ///
        ///     Post    :- returns error code if the item isnt in the database
        ///                otherwise return success code 200
        ///     
        /// </summary>
        public void DeleteListing(String idInput)
        {
            if (clientTable.DeleteListing(idInput) != true)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            // default is to return 200 OK
        }
        /// <summary>
        /// 
        ///     The POST function which ADD or OVERWRITE an item in the database
        ///             
        ///     Pre     :- The database connection must be open
        ///                input value must contain a valid RowKey 
        ///                that is not in the database
        ///
        ///     Post    :- returns error code if the item isnt in the database
        ///                otherwise return success code 200
        ///     
        /// </summary>
        public HttpResponseMessage PostAddListing(StockDetails input)
        {
            if (clientTable.AddListing(input) < 300 )
                return Request.CreateResponse<StockDetails>(HttpStatusCode.Created, input);

            return Request.CreateResponse<StockDetails>(HttpStatusCode.NotModified, input);
        }
        /// <summary>
        /// 
        ///     The PUT function which MODIFY an item in the database
        ///             
        ///     Pre     :- The database connection must be open
        ///                idInput or listing value must contain a valid RowKey 
        ///                that is in the database
        ///
        ///     Post    :- returns error code if the item isnt in the database
        ///                otherwise return success code 200
        ///     
        /// </summary>
        public void PutUpdateListing(String idInput, StockDetails listing)
        {
            if (idInput == listing.ProductIdSymbol)
            {
                if (clientTable.UpdateListing(listing) == true)
                    return;
                else
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            else
            {
                if (clientTable.UpdateListing(idInput, listing) == true)
                    return;
                else
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
        }
    }
}
